using QFramework;
using QFramework.Game;
using QGameFramework.Models;
using QGameFramework.SO;
using QGameFramework.Tools;
using UnityEngine;

namespace QGameFramework.QConfig
{
    public class ConfigComponent : BaseComponent
    {
        [SerializeField] private GameConfigSO GCSO;

        public override void OnInit()
        {
            base.OnInit();
            SetConfig();
        }

        void SetConfig()
        {
            var m_GC = this.GetModel<IGameConfig>();
#if UNITY_EDITOR
            SetInit(m_GC);
# elif UNITY_STANDALONE_WIN
            QGFC.Instance.Tool.m_Tool.ReadFormFile<GameConfig>(Application.streamingAssetsPath + "/Config/Config.ini", out string info, ReadCallback);
            QGFC.Instance.Tool.m_Tool.Write2File<GameConfig>(Application.streamingAssetsPath + "/Config/Config.ini", (GameConfig)m_GC);
# elif UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS
            QGFC.Instance.Tool.m_Tool.ReadFormFile<GameConfig>(Application.persistentDataPath + "/Config/Config.ini", out string info,ReadCallback);
            QGFC.Instance.Tool.m_Tool.Write2File<GameConfig>(Application.persistentDataPath + "/Config/Config.ini", (GameConfig)m_GC);
#endif
        }

        private void SetInit(IGameConfig m_GC)
        {
            m_GC.MaxResourcesCount = GCSO.m_ResourcesSize;
            m_GC.CleanTime = GCSO.m_CleanTime;

            m_GC.MusicValue = GCSO.m_MusicValue;
            m_GC.SoundValue = GCSO.m_SoundValue;
            m_GC.VoiceValue = GCSO.m_VoiceValue;

            m_GC.OpenMusic = GCSO.m_OpenMusic;
            m_GC.OpenSound = GCSO.m_OpenSound;
            m_GC.OpenVoice = GCSO.m_OpenVoice;

            m_GC.IpAdr = GCSO.m_IpAdr;
            m_GC.Port = GCSO.m_Port;

            m_GC.UIResolution = GCSO.m_UIResolution;
        }


        private void ReadCallback(string info, GameConfig config)
        {
            var m_GC = this.GetModel<IGameConfig>();

            if (info == string.Empty)
            {
                SetInit(m_GC);
            }
            else
            {
                m_GC.MaxResourcesCount = config.MaxResourcesCount;
                m_GC.CleanTime = config.CleanTime;

                m_GC.MusicValue = config.MusicValue;
                m_GC.SoundValue = config.SoundValue;
                m_GC.VoiceValue = config.VoiceValue;

                m_GC.OpenMusic = config.OpenMusic;
                m_GC.OpenSound = config.OpenSound;
                m_GC.OpenVoice = config.OpenVoice;

                m_GC.IpAdr = config.IpAdr;
                m_GC.Port = config.Port;
                m_GC.UIResolution = config.UIResolution;
            }
        }

        private void OnDestroy()
        {
            var m_GC = this.GetModel<IGameConfig>();
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
            QGFC.Instance.Tool.m_Tool.Write2File<GameConfig>(Application.streamingAssetsPath + "/Config/Config.ini", (GameConfig)m_GC);
#elif UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS
           QGFC.Instance.Tool.m_Tool.Write2File<GameConfig>(Application.persistentDataPath + "/Config/Config.ini", (GameConfig)m_GC);
#endif
        }
    }
}